package org.javaparser.template;

import japa.parser.ASTHelper;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.expr.AssignExpr;
import japa.parser.ast.expr.Expression;
import japa.parser.ast.expr.FieldAccessExpr;
import japa.parser.ast.expr.NameExpr;
import japa.parser.ast.expr.StringLiteralExpr;
import japa.parser.ast.expr.ThisExpr;
import japa.parser.ast.expr.AssignExpr.Operator;
import japa.parser.ast.stmt.BlockStmt;
import japa.parser.ast.stmt.ReturnStmt;

public class MethodCodeWrapper {
	private CompilationUnit cu;
	private ClassOrInterfaceDeclaration type; 
	private BlockStmt block;
	private String currentPackageName;

	public MethodCodeWrapper(CompilationUnit cu, ClassOrInterfaceDeclaration type, BlockStmt block) {
		this.cu = cu;
		this.type = type;
		this.block = block;
		determineCurrentPackageName();
	}

	private void determineCurrentPackageName() {
		this.currentPackageName = cu.getPackage().getName().getName();
	}

	public void addVariableReturn(String variableName, boolean useThis) {
        FieldAccessExpr fieldAccessExpr = new FieldAccessExpr(useThis ? new ThisExpr() : null, variableName);
        ReturnStmt stmt = new ReturnStmt(fieldAccessExpr);
        ASTHelper.addStmt(block, stmt);
	}

	public void addStringReturn(String stringValue) {
        ReturnStmt stmt = new ReturnStmt(new StringLiteralExpr(stringValue));
        ASTHelper.addStmt(block, stmt);
	}

	public void addNullReturn() {
        ReturnStmt stmt = new ReturnStmt(new NameExpr("null"));
        ASTHelper.addStmt(block, stmt);
	}

	public void addVariableAffectation(String targetVariableName,
					boolean targetUseThis, String variableName, boolean useThis) {
        Expression targetVariableExpr = null;
        if (targetUseThis) {
        	targetVariableExpr = new FieldAccessExpr(targetUseThis ? new ThisExpr() : null, targetVariableName);
        } else {
        	targetVariableExpr = new NameExpr(targetVariableName);
        }

        Expression variableExpr = null;
        if (useThis) {
        	variableExpr = new FieldAccessExpr(targetUseThis ? new ThisExpr() : null, variableName);
        } else {
        	variableExpr = new NameExpr(variableName);
        }

        AssignExpr assignExpr = new AssignExpr(targetVariableExpr, variableExpr, Operator.assign);
        ASTHelper.addStmt(block, assignExpr);
	}

}
